﻿--[[
   @brief    GameTips
   @author   seanyxie,hanmanhuang
   @version  1.0
   @date     2016-04-25
--]]

base_utility.log("loading gametips.lua");

function Split(szFullString, szSeparator)
    local nFindStartIndex = 1;
    local nSplitIndex = 1;
    local nSplitArray = {};
    while true do
       local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex);
       if not nFindLastIndex then
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString));
        break;
       end;
       nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1);
       nFindStartIndex = nFindLastIndex + string.len(szSeparator);
       nSplitIndex = nSplitIndex + 1;
    end;
    return nSplitArray;
end;


NUIBannerTipsEventListener = {

    close_click_tips = function(UILib, control_id, x, y)
        local btn_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
        local tips_ctrl_id = ui_utility.get_ctrl_parent_id(btn_ctrl_id);

        tips = GameTipsMgr.get_tips_by_ctrlid(tips_ctrl_id);
        tips:close_tips();
    end;

    clicked_tips = function(UILib, control_id, x, y)
        local tips_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
        tips = GameTipsMgr.get_tips_by_ctrlid(tips_ctrl_id);
        tips:click_tips();
    end;

    on_banner_tips_hover = function(uilib, control_id, x, y, id_message_from)
        base_utility.log(
            string.format("[NUIBannerTipsEventListener.on_banner_tips_hover]control_id is %d",
            control_id));
        local tips_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
        tips = GameTipsMgr.get_tips_by_ctrlid(tips_ctrl_id);
        if tips == nil then
            base_utility.log("[NUIBannerTipsEventListener.on_banner_tips_hover]tips obj is nil");
            return;
        end
        tips:on_hover();
    end;

    on_banner_tips_leave = function(uilib, control_id, x, y, id_message_from)
        base_utility.log(
            string.format("[NUIBannerTipsEventListener.on_banner_tips_leave]control_id is %d",
            control_id));
        local tips_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
        tips = GameTipsMgr.get_tips_by_ctrlid(tips_ctrl_id);
        if tips == nil then
            base_utility.log("[NUIBannerTipsEventListener.on_banner_tips_hover]tips obj is nil");
            return;
        end
        tips:on_leave();
    end;
};

GameTips = {
};

function GameTips:new (game_id, wrapper_id, tipslist, tipscount, banner)
    base_utility.log(string.format("GameTips:new %d", wrapper_id));
    local o = {game_id_ = game_id, wrapper_id_ = wrapper_id, banner_ = banner};
    setmetatable(o, self);
    self.__index = self;
    o:init();
    o:set_tips_content(tipslist,tipscount);

    return o;
end;

function GameTips:init()
    self.uilib = ui_utility.uilib();
    -- Set game id in CustomData of root ctrl
    self.banner_tips_id_ = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "CntBannerTips");
    self.banner_tips_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.banner_tips_id_);

    self.tips_btn_id_ = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "CntBannerTips\\BtnTips");
    self.tips_btn_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.tips_btn_id_);

    self.tips_txt_id_ = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "CntBannerTips\\BtnTips\\TxtTips");

    self.close_btn_id_ = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "CntBannerTips\\BtnTips\\BtnClose");
    self.close_btn_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.close_btn_id_);

    self.is_hover_ = false;
end;

function GameTips:log(msg, core_msg)
    base_utility.log(string.format("gametips %s", msg), core_msg);
end;

--[[
    struct tagGameTip
{
    int32_t iGame_id;                                     /*   游戏ID */
    uint32_t dwMsg_id;                                    /*   消息ID */
    char szDis_type[RES_MAX_NAME_LEN];                    /*   显示类型 */
    uint32_t dwPriority;                                  /*   优先级,数字越大优先级越高 */
    char szColor[RES_MAX_NAME_LEN];                       /*   广告栏颜色 */
    char szMsg[RES_MAX_TIP_MSG_LEN];                      /*   文字 */
    char szUrl[RES_MAX_URL_LEN];                          /*   链接地址 */
    char szBegin_time[RES_MAX_TIME_TEXT_LEN];             /*   开始时间 */
    char szEnd_time[RES_MAX_TIME_TEXT_LEN];               /*   结束时间 */
};

tipslist 是tagGameTips[3]的数组
tipslist.count
tipslist.content 是表
--]]

function GameTips:set_tips_content(tipslist,count)
    self.tipscount = count;
    if count == 1 then
        self.tips1 = tipslist[1];
        self.tips2 = nil;
        self.tips3 = nil;
    elseif count ==2 then
        self.tips1 = tipslist[1];
        self.tips2 = tipslist[2];
        self.tips3 = nil;
    elseif count == 3 then
        self.tips1 = tipslist[1];
        self.tips2 = tipslist[2];
        self.tips3 = tipslist[3];
    else
        self.tips1 = nil;
        self.tips2 = nil;
        self.tips3 = nil;
    end;
    
    self:update_current_tips();
end;

-- 主动或者定时器驱动，更新当前tips内容
function GameTips:update_current_tips()
    all_tips = {self.tips1, self.tips2, self.tips3};
    local invalid_tips = {};
    local count=0;

    -- 判断是否在需要显示时间内
    if self.tipscount >= 1 then
        for i=1,self.tipscount do 
            if GameTipsMgr.is_tips_can_show_now(all_tips[i]) == true then
                count = count+1;
                invalid_tips[count] = all_tips[i];
            end;
        end;
    end;

    -- 比较所有有效的tips ，选取优先级最高的那个
    local init_priority = 0;
    local index =1;
    if count >=1 then
        for i=1,count do
            if invalid_tips[i].dwPriority > init_priority then
                init_priority = invalid_tips[i].dwPriority;
                index=i;
            end;
        end;
    else
        --没有一个在有效时间内
        index = 0;
    end;
    

    if index ~= 0 then
        -- 显示 invalid_tips[index]
        self:set_current_tips(invalid_tips[index]);    
        self:show_current_gametips(TRUE);

        -- 到这里认为Tips已经完全显示出来了；QOS上报
        self:sendshown_to_qos(invalid_tips[index].dwMsg_id);
    else 
        -- 当前没有要显示的，隐藏当前gametips
        self:show_current_gametips(FALSE);
    end;

end;

-- 上传Mini界面GameTips曝光量QOS
function GameTips:sendshown_to_qos(dw_msgid)
    local b_will_send = false;
    local dw_gameid = self.game_id_;
    local dw_msgid = dw_msgid;
    if nil ~= dw_gameid and nil ~= dw_msgid then
        if nil ~= banner_model.game_tips_shown and
                    table.getn(banner_model.game_tips_shown) > 0 then
            local length = table.getn(banner_model.game_tips_shown);
            local b_has_saved = false;
            local gameid_index = 1;
            for i = 1, length do
                if dw_gameid == banner_model.game_tips_shown[i].saved_gameid then
                    b_has_saved = true;
                    gameid_index = i;
                    break;
                end;
            end;
            if b_has_saved == false then
                -- 没有写过该游戏ID
                b_will_send = true;
                local param = {};
                param.saved_gameid = dw_gameid;
                param.saved_msgid = dw_msgid;
                param.saved_minshow = 1;
                banner_model.game_tips_shown[length + 1] = param;
            else
                -- 写过，判断big_show字段
                local has_minshow = banner_model.game_tips_shown[gameid_index].saved_minshow;
                if nil ~= has_minshow and 1 == has_minshow then
                    -- 显示过，判断msgid
                    local shown_msgid = banner_model.game_tips_shown[gameid_index].saved_msgid;
                    if nil ~= shown_msgid and dw_msgid == shown_msgid then
                        b_will_send = false;
                    else
                        banner_model.game_tips_shown[gameid_index].saved_msgid = dw_msgid;
                        b_will_send = true;
                    end;
                else
                    -- 没有显示过
                    banner_model.game_tips_shown[gameid_index].saved_minshow = 1;
                    banner_model.game_tips_shown[gameid_index].saved_msgid = dw_msgid;
                    b_will_send = true;
                end;
            end;
             
        else
            base_utility.log("[GameTips:sendshown_to_qos]game_tips_shown is nil");
            banner_model.game_tips_shown = {};
            local param = {};
            param.saved_gameid = dw_gameid;
            param.saved_msgid = dw_msgid;
            param.saved_minshow = 1;
            banner_model.game_tips_shown[1] = param;
            b_will_send = true;
        end;

        if b_will_send == true then
            -- 发送曝光量qos
            tqos_mgr.report_game_tips(dw_gameid, 1, 0, dw_msgid);
        end;
    else
        base_utility.log("[GameTips:sendshown_to_qos]no gameid or msgid");
    end;
end;

function GameTips:set_current_tips(current_tips)
    base_utility.log("set_current_tips --------.");

    self.current_tips = current_tips;
    if self.current_tips == nil then
        return;
    end
    -- 设置文字
    ui_utility.setproperty_by_id(self.tips_txt_id_,'Caption',self.current_tips.szMsg);

      -- 按钮颜色
      if self.current_tips.szColor == "Green" or
       self.current_tips.szColor == "Blue" then
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "Background","COLOR:RGB:19,64,146");
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "BackgroundHover","COLOR:RGB:19,64,146");
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "BackgroundPressed","COLOR:RGB:19,64,146");
      else  --self.current_tips.szColor=="Red"
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "Background","COLOR:RGB:149,19,19");
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "BackgroundHover","COLOR:RGB:149,19,19");
          ui_utility.setproperty_by_id(self.tips_btn_id_,
            "BackgroundPressed","COLOR:RGB:149,19,19");
      end;

    -- 文字颜色
    self:update_tips_color();

    -- 关闭按钮状态
    self:update_close_btn_status();
end;

function GameTips:update_tips_color()
    if self.current_tips == nil then
        return;
    end
    if self.current_tips.szColor == "Green" or
       self.current_tips.szColor == "Blue" then
        if self.is_hover_ then
            ui_utility.setproperty_by_id(self.tips_txt_id_,
            "TextColor","RGB:216,230,255");
        else
            ui_utility.setproperty_by_id(self.tips_txt_id_,
            "TextColor","RGB:165,197,255");
        end
    else
        if self.is_hover_ then
            ui_utility.setproperty_by_id(self.tips_txt_id_,
            "TextColor","RGB:255,255,104");
        else
            ui_utility.setproperty_by_id(self.tips_txt_id_,
            "TextColor","RGB:255,206,89");
        end
    end;
end;

function GameTips:show_current_gametips(show)
    base_utility.log("close_tips --------.");
    --[[
    local this_ctrl = self.uilib:GetControlPtr(self.ctrl_id_);
    if this_ctrl == nil then 
        return;    
    end;
    this_ctrl:Show(show);
    --]]

    if nil ~= self.banner_ then
        self.banner_:game_tips_visible_change(show);
    end

    local tips_wrapper_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.wrapper_id_);
    if tips_wrapper_ptr_ ~= nil then
        tips_wrapper_ptr_:Show(show);
    end

--[[
    if  show ~= self.notified_visible_ then
      self.notified_visible_ = show;
        parent_ctrl:Show(show);
        param ={};
        param["game_id"] = self.game_id_;
        local sh = false;
        if show == TRUE then
            sh = true;
        end;
        param["visible"] = sh;
        event_center.send_event(main_event.GAME_TIP_VISIBLE_CHANGED,base_utility.table_to_json(param));
    end;
--]]
end;

function GameTips:close_tips()
    base_utility.log("close_tips --------.");
      self:show_current_gametips(FALSE);
      local gamelib =
       base_utility.get_component_adapter("IGame_library",
           "ierd_tgp.game_library.Get_game_library_adapter();");
    gamelib:recordGameTipViewed(self.current_tips.dwMsg_id, self.current_tips.iGame_id);
end;

function GameTips:click_tips()
      local gamelib =
       base_utility.get_component_adapter("IGame_library",
           "ierd_tgp.game_library.Get_game_library_adapter();");
    gamelib:recordGameTipViewed(self.current_tips.dwMsg_id,self.current_tips.iGame_id);

    -- 上报QOS
    tqos_mgr.report_game_tips(self.current_tips.iGame_id, 1, 1, self.current_tips.dwMsg_id);
    --发出事件到 open_link
    web_utility.open_link(self.current_tips.szUrl);
--[[
    qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
    qosparam.oc_type = 1;
    qosparam.qos_report_id = 145; --url上报
    qosparam.int_count = 3;
    qosparam.str_count =0; 
    qosparam.int_val[0]= 9;
    qosparam.int_val[1]= self.current_tips.iGame_id;
    qosparam.int_val[2]= self.current_tips.dwMsg_id;
    base_utility.lua_qos(qosparam);
    qosparam:delete();
--]]
    if self.current_tips.szDis_type ~="Always" then
        self:show_current_gametips(FALSE);
    else
        self:show_current_gametips(TRUE);
    end;
    base_utility.log("click_tips --------.");
end;


function GameTips:tick_count()
     self:update_current_tips();
end;

function GameTips:update_close_btn_status()
    if self.current_tips == nil then
        return;
    end
    if self.is_hover_ then
        -- 设置是否可关闭
          if self.current_tips.szDis_type =="Everyday" then
              self.close_btn_ptr_:Show(TRUE);
          elseif self.current_tips.szDis_type =="Always" then
              self.close_btn_ptr_:Show(FALSE);
          else
           -- 其他非法值 作为once
              self.close_btn_ptr_:Show(TRUE);
          end;
    else
        self.close_btn_ptr_:Show(FALSE);
    end
end;

function GameTips:on_hover()
    self.is_hover_ = true;
    self:update_tips_color();
    self:update_close_btn_status();
end;

function GameTips:on_leave()
    self.is_hover_ = false;
    self:update_tips_color();
    self:update_close_btn_status();
end;


